package systems;

import org.newdawn.slick.Input;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;

import components.Player;
import components.Velocity;
import components.Position;
import components.Thruster;

import gamestates.PlayState;

import com.artemis.ComponentMapper;
import com.artemis.EntityProcessingSystem;
import com.artemis.Entity;
public class InputSystem extends EntityProcessingSystem
{
	private ComponentMapper<Player> player;
	private ComponentMapper<Position> pos;
	private ComponentMapper<Velocity> vel;
	private ComponentMapper<Thruster> thrust;
	private boolean lastSpace = false;
	private GameContainer gc;
	
	public InputSystem(GameContainer gc)
	{
		super(Player.class, Velocity.class, Position.class, Thruster.class);
		this.gc = gc;
	}
	
	public void initialize()
	{
		player = new ComponentMapper<Player>(Player.class, world);
		pos = new ComponentMapper<Position>(Position.class, world);
		vel = new ComponentMapper<Velocity>(Velocity.class, world);
		thrust = new ComponentMapper<Thruster>(Thruster.class, world);
	}
	
	public void process(Entity e)
	{
		Input input = gc.getInput();
		System.out.println("PosX:" + pos.get(e).getX());
		System.out.println("PosY:" + pos.get(e).getY());
		System.out.println("VelX:" + vel.get(e).getX());
		System.out.println("VelY:" + vel.get(e).getY());
		System.out.println("Angle" + pos.get(e).getTheta());
		if(input.isKeyDown(Input.KEY_UP))
		{
			vel.get(e).setX((float) (Math.cos(Math.toRadians(pos.get(e).getTheta())) * player.get(e).getAcceleration() * world.getDelta() + vel.get(e).getX()));
			vel.get(e).setY((float) (Math.sin(Math.toRadians(pos.get(e).getTheta())) * player.get(e).getAcceleration() * world.getDelta() + vel.get(e).getY()));
			
			float magnitude = vel.get(e).getX() * vel.get(e).getX() + vel.get(e).getY() * vel.get(e).getY();
			if(magnitude > vel.get(e).getMax())
			{
				vel.get(e).setX(vel.get(e).getMax() / magnitude * vel.get(e).getX());
				vel.get(e).setY(vel.get(e).getMax() / magnitude * vel.get(e).getY());
			}
			thrust.get(e).setThrusting(true);
		}
		
		else
		{
			thrust.get(e).setThrusting(false);
		}
		
		/*if(input.isKeyDown(Input.KEY_DOWN))
		{
				vel.get(e).setX((float) (-Math.cos(Math.toRadians(pos.get(e).getTheta())) * player.get(e).getAcceleration() * 2 / 3 * world.getDelta() + vel.get(e).getX()));
				vel.get(e).setY((float) (-Math.sin(Math.toRadians(pos.get(e).getTheta())) * player.get(e).getAcceleration() * 2 / 3 * world.getDelta() + vel.get(e).getY()));
		}*/
		
		if(input.isKeyDown(Input.KEY_LEFT))
		{
			pos.get(e).setTheta(pos.get(e).getTheta() - world.getDelta() * player.get(e).getTurn_speed());
		}
		
		if(input.isKeyDown(Input.KEY_RIGHT))
		{
			pos.get(e).setTheta(pos.get(e).getTheta() + world.getDelta() * player.get(e).getTurn_speed());
		}
		
		if(input.isKeyDown(Input.KEY_SPACE) && !lastSpace)
		{
			try {
				PlayState.getInstance().createMedDebris(e);
			} catch (SlickException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		if(input.isKeyDown(Input.KEY_SPACE))
		{
			lastSpace = true;
		}
		else
		{
			lastSpace = false;
		}
	}

}
